% GAUTIER LE BIHAN - 2020
% Replication files for "Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model?" Review of Economics and Statistics
%
% TABLE D

clear;
tic
addpath('..\..\Utilities')  
load actual_moments_k


weight=actual_moments_k(:,12);
sum(weight)
load ..\..\Estimation_param\MS_produits_CPlus\param
param0=param

param=[param0];
s=227

    p0=tanh(param(:,2));
    muc=param(:,3);
    sig_ea=exp(param(:,4));
   p_a=tanh(param(:,5));
    rho_a=0.6946*ones(size(param,1),1); 
    



param0=[p0 muc sig_ea p_a rho_a]

size_param=size(param0,2)


stat_med=ones(1,size_param+2);
%figure;hist(param_sect(:,3));
v=size(param0,1);
 if v>0
    for i=1:size(param0,2)
        [sortx,order] = sort(param0(:,i));
        sortw = weight(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
         
        if isempty(j)==1
             j=1;
         end
            if csumw(j) == midpoint
              stat_med(i) = mean(sortx([j j+1]));
            else
                
                stat_med(i) = sortx(j+1);
            end
     stat_med(end)= csumw(end);
     stat_med(end-1)= v;
    end  
 end

stat_med

s=227;
for pp=1:4

param_t=param0;
param_t(:,pp)=stat_med(:,pp);

    stat=zeros(s,17);
    for jj=1:s;
        p0=param_t(jj,1);
        mu_c=param_t(jj,2);
        sig_eps_a=param_t(jj,3);
        p_a=param_t(jj,4);
        rho_a=param_t(jj,5);   
        weight_j=weight(jj);
        sm_m=0;
        param_e=[p0 mu_c sig_eps_a p_a rho_a];
        [moments, VC]=geNCalvoPlus_VC(p0, mu_c, sig_eps_a,p_a, weight_j);
        stat(jj,:)=[ param_e moments VC];
	jj
       
    end
   stat_simu_outb(pp).stat_simu_outb=stat;
   save stat_simu_outb stat_simu_outb;
end